package com.so206.controller.pay.process;

import com.so206.plugins.pay.AliPayFunction;
import com.so206.po.AlipayConfig;
import com.so206.po.AlipayTradeRecord;
import com.so206.service.AlipayTradeServices;
import com.so206.utils.PayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 支付宝 -- 资金归集线程
 */
public class AlipayTradeThread implements Runnable {

    private static final Logger logger = LoggerFactory.getLogger(AlipayTradeThread.class);

    private AliPayFunction aliPayFunction;

    private AlipayConfig alipayConfig;

    private String tr_name;

    private String tr_account;

    private String tr_desc;

    private BigDecimal tr_money;

    private BigDecimal tr_sett;

    private AlipayTradeServices alipayTradeServices;

    public AlipayTradeThread() {
        logger.info("支付宝资金归集...");
    }

    public AlipayTradeThread(AliPayFunction aliPayFunction, AlipayConfig alipayConfig, String tr_name, String tr_account, String tr_desc, BigDecimal tr_money, BigDecimal tr_sett, AlipayTradeServices alipayTradeServices) {
        this.aliPayFunction = aliPayFunction;
        this.alipayConfig = alipayConfig;
        this.tr_name = tr_name;
        this.tr_account = tr_account;
        this.tr_desc = tr_desc;
        this.tr_money = tr_money;
        this.tr_sett = tr_sett;
        this.alipayTradeServices = alipayTradeServices;
    }

    @Override
    public void run() {
        logger.info("开始打款...");
        BigDecimal sub = tr_money.multiply(tr_sett).setScale(2, BigDecimal.ROUND_HALF_UP);
        String oid = PayUtils.genOrderId();
        AlipayTradeRecord record = new AlipayTradeRecord();
        record.setOptTime(new Date());
        record.setCreateTime(new Date());
        record.setErrInfo("");
        record.setOutOrderId(oid);
        record.setSett(tr_sett);
        record.setSettChange(sub);
        record.setTrMoney(tr_money);
        record.setStatus(0);
        record.setTrAccount(tr_account);
        record.setTradeNo("");
        record.setTrName(tr_name);
        record.setTrDesc(tr_desc);
        record.setConnId(alipayConfig.getId());
        record.setTradeStatus("");
        Long record_id = alipayTradeServices.save_record(record);
        HashMap<String, String> param = new HashMap<>();
        param.put("final_money", String.valueOf(tr_money.subtract(sub)));
        param.put("sid", oid);
        param.put("pid", tr_account);
        param.put("pname", tr_name);
        param.put("ip", "127.0.0.1");
        param.put("title", tr_desc);
        Map money = aliPayFunction.changeMoneyToAccountMap(param, alipayConfig.getAppId(), alipayConfig.getPrivateKey(), alipayConfig.getPublicKey());
        if (money == null) {
            logger.info("请求支付宝失败,原因未知!");
            return;
        }
        Object error = money.get("error");
        Integer status = (Integer) money.get("status");
        Object tran_no = money.get("tran_no");
        AlipayTradeRecord record1 = alipayTradeServices.find_record_by_id(record_id);
        if (status == null) {
            logger.info("请求支付宝失败,原因:" + error);
            record1.setStatus(0);
            record1.setTradeStatus("ERROR");
            record1.setErrInfo(String.valueOf(error));
            alipayTradeServices.update_record(record1);
            return;
        }
        if (status == 2) {
            record1.setStatus(1);
            record1.setTradeStatus("SUCCESS");
            record1.setErrInfo("SUCCESS");
            record1.setTradeNo(String.valueOf(tran_no));
        } else {
            record1.setStatus(0);
            record1.setTradeStatus("ERROR");
            record1.setErrInfo(String.valueOf(error));
        }
        alipayTradeServices.update_record(record1);
        logger.info("打款结束,状态:" + status);
    }


}
